var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    class_catagory: [],
    class_country: [],
    list: [],

    myhost: app.globalData.remoteDomainMy,

    all_active_class:'active-class',
    all_active_country: 'active-country',

    current_class:'all_class',
    current_country: 'all_country'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
      //请求数据
      wx.request({
        url: app.globalData.remoteDomainApi + 'listmovie.php',
        data: {
          x: '',
          y: ''
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
          //给模板提供数据
          that.setData({
            list: res.data.list_datas,
            class_catagory: res.data.class_catagory,
            class_country: res.data.class_country
          })
        }
      })
  },


  /**
   * 选择分类
   */
  classSelect:function(e){
    var that = this;
    // 当前电影分类
    let currentSelectedClass = e.currentTarget.dataset.selectedclass;
        // 改变当前分类状态
        if(currentSelectedClass == 'all_class'){
          var newClass= 'active-class'
        }else{
          var newClass = ''
        }

        this.setData({
          current_class:currentSelectedClass,
          all_active_class:newClass
        })


    // 当前电影地区
    let currentSelectedCountry = this.data.current_country;

    // 根据这两个条件读取API数据
    wx.request({
      url: app.globalData.remoteDomainApi + 'classmovie.php',
      data: {
        cls: currentSelectedClass,
        country: currentSelectedCountry
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {       
        that.setData({
          list: res.data.list_datas,
          class_catagory: res.data.class_catagory,
          class_country: res.data.class_country
        })
      }
    })
  },


  /**
   * 选择国家
   */
  countrySelect: function (e) {
    var that = this;

    // 当前电影分类
    let currentSelectedClass = this.data.current_class;
      
    // 当前电影地区
    let currentSelectedCountry = e.currentTarget.dataset.selectedcountry;

    if (currentSelectedCountry == 'all_country') {
      var newCountry = 'active-country'
    } else {
      var newCountry = ''
    }
    this.setData({
      current_country: currentSelectedCountry,
      all_active_country: newCountry
    })

    // 根据这两个条件读取API数据
    wx.request({
      url: app.globalData.remoteDomainApi + 'classmovie.php',
      data: {
        cls: currentSelectedClass,
        country: currentSelectedCountry
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        that.setData({
          list: res.data.list_datas,
          class_catagory: res.data.class_catagory,
          class_country: res.data.class_country
        })
      }
    })
  },
  /**
  * 电影详情
  */
  gomoviedetail: function (e) {
    let id = e.currentTarget.dataset.id;

    wx.navigateTo({
      url: '../detailMovie/detailMovie?id=' + id,
    })
  }
})